package com.dwxt.cdms.applets.service;

import com.dwxt.cdms.dao.LbWatchLocMapper;
import com.dwxt.cdms.dao.yumeng.LocDataMapper;
import com.dwxt.cdms.entity.oufu.OuFuLocData;
import com.dwxt.cdms.service.EquipmentCustomService;
import com.dwxt.cdms.service.OuFuService;
import com.dwxt.common.base.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: TODO
 * @className: LocService
 * @author: yanlong
 * @createDate: 2021/05/25 16:49
 * @version: v1.0
 */
@Service
public class LocService {

    @Autowired
    private EquipmentCustomService equipmentCustomService;

    @Autowired
    private LocDataMapper locDataMapper;

    @Autowired
    private OuFuService ouFuService;

    @Autowired
    private LbWatchLocMapper lbWatchLocMapper;

    public BaseResult getLoc(Integer customerId, String startTime, String endTime) {
        if (startTime.length() <= 10) {
            startTime = startTime + " 00:00:00";
        }
        if (endTime.length() <= 10) {
            endTime = endTime + " 23:59:59";
        }

        //合并欧孚设备定位轨迹
        List<OuFuLocData> oufuList = ouFuService.selectAllLocDataListByCustomerIdAndTime(customerId, startTime, endTime);
        List<OuFuLocData> lbList = lbWatchLocMapper.selectLocListByCustomerIdAndTime(customerId, startTime, endTime);
        if (oufuList != null && lbList != null) {
            oufuList.addAll(lbList);
            List<OuFuLocData> collect = oufuList.stream().sorted(Comparator.comparing(OuFuLocData::getCreateTime)).collect(Collectors.toList());
            return BaseResult.ok(collect);
        } else if (oufuList != null ) {
            List<OuFuLocData> collect = oufuList.stream().sorted(Comparator.comparing(OuFuLocData::getCreateTime)).collect(Collectors.toList());
            return BaseResult.ok(collect);
        } else if (lbList != null) {
            List<OuFuLocData> collect = lbList.stream().sorted(Comparator.comparing(OuFuLocData::getCreateTime)).collect(Collectors.toList());
            return BaseResult.ok(collect);
        }
        return BaseResult.ok();
    }

    public BaseResult getCardLoc(Integer customerId,String imei, String startTime, String endTime) {
        if (startTime.length() <= 10) {
            startTime = startTime + " 00:00:00";
        }
        if (endTime.length() <= 10) {
            endTime = endTime + " 23:59:59";
        }
        List<OuFuLocData> ouFuLocDataList = ouFuService.selectCardLocDataListByImeiAndTime(customerId,imei, startTime, endTime);
        return BaseResult.ok(ouFuLocDataList);

    }
}
